/**
 * Created by apple on 2016/11/7.
 */
angular.module('starter.userController')
  .factory("$update", function ($dfHttp,$cordovaAppVersion,$cordovaDialogs,$state){

    function update(){
      if(!window.cordova || !window.cordova.hasOwnProperty('getVersionNumber')) {
        $state.go("login", {}, {animation: "no-animation"});
        return ;
      }
      $cordovaAppVersion.getVersionNumber().then(function(versionCode){
        $dfHttp.get('api/others/getNowVersion.json')
          .success(function(data){
            if (data && data.code === 'SUCCESS') {
              if (angular.isArray(data.data)) {
                for(var index = 0 ; index < data.data.length ; index++){
                  var platform = data.data[index];
                  if (platform.versionPlatform == 'Android' && ionic.Platform.isAndroid()) {
                    if (platform.version != versionCode ) {
                      showUpdateAndroid(platform.enforceUpdate == 'Y',platform.versionUrl);
                      return ;
                    }
                  } else if(platform.versionPlatform == 'IOS' && ionic.Platform.isIOS()) {
                    if (platform.version != versionCode ) {
                      showUpdateIos(platform.enforceUpdate == 'Y',platform.versionUrl);
                      return ;
                    }
                  }
                }
              }
            }
            $state.go("login", {}, {animation: "no-animation"});
          }).error(function(data){
          $state.go("login", {}, {animation: "no-animation"});
        });
      });
    }

    function showUpdateAndroid(fouce,url,errorCallback) {
      if (fouce) {
        $cordovaDialogs.alert('应用有新的版本了,请点击更新。','版本更新','确认更新').then(function(buttonIndex){
          window.open(url,'_system');
          showUpdateAndroid(fouce,url);
        });
      }else {
        $cordovaDialogs.confirm('应用有新的版本了,请点击更新。','版本更新',['取消','确认更新']).then(function(buttonIndex){
          if (buttonIndex == 2) {
            window.open(url,'_system');
          } else {
            $state.go("login", {}, {animation: "no-animation"});
          }
        });
      }
    }

    function showUpdateIos(fouce,url) {
      if (fouce) {
        $cordovaDialogs.alert('应用有新的版本了,请点击更新。','版本更新','确认更新').then(function(buttonIndex){
          if (url.indexOf("plist") >= 0 || url.indexOf("https://www.pgyer.com/app/plist") >= 0) {
            window.open('itms-services://?action=download-manifest&url=' + url, '_system');
          } else {
            window.open(url, '_system');
          }
          showUpdateIos(fouce,url);
        });
      }else {
        $cordovaDialogs.confirm('应用有新的版本了,请点击更新。','版本更新',['取消','确认更新']).then(function(buttonIndex){
          if (buttonIndex == 2) {
            if (url.indexOf("plist") >= 0 || url.indexOf("https://www.pgyer.com/app/plist") >= 0) {
              window.open('itms-services://?action=download-manifest&url=' + url, '_system');
            } else {
              window.open(url, '_system');
            }
          }else {
            $state.go("login", {}, {animation: "no-animation"});
          }
        });
      }
    }

    return {
      update:update
    }
  })
